package com.chapter_6;

import java.util.Random;

/**
 * @Author: Timo
 * @Date: 2019/5/16 12:24
 * @Description: 写入的线程操作类
 */
public class WriterThread extends Thread {

    private static final Random random = new Random();
    private final Data data;
    private final String filler;
    private int index = 0;

    public WriterThread(Data data, String filler) {
        this.data = data;
        this.filler = filler;
    }

    @Override
    public void run() {
        try {
            while (true) {
                data.write(nextChar());
                Thread.sleep(5_000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    private char nextChar() {
        char c = filler.charAt(index);
        index = (index + 1) % filler.length();
        return c;
    }
}
